﻿@page
@model ToolGood.FlowWork.Pages.Analysis.FlowDebugResultModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>流程调试</title>
    <link href="/_/layui/css/layui.css" rel="stylesheet" />
    <link href="/_/css/layform_sm.css" rel="stylesheet" />
    <link href="/_/x6/x6.css" rel="stylesheet" />
    <script src="/_/js/jquery.min.js"></script>
    <script src="/_/X6/x6.js"></script>
    <style>
        #container { display: flex; border: 1px solid #dfe3e8; }
        #graph-container { width: 100%; height: 100%; }
        .node { display: flex; align-items: center; width: 100%; height: 100%; background-color: #fff; border: 1px solid #c2c8d5; border-left: 4px solid #5F95FF; border-radius: 4px; box-shadow: 0 2px 5px 1px rgba(0, 0, 0, 0.06); }
        .node img { width: 20px; height: 20px; flex-shrink: 0; margin-left: 8px; }
        .node .label { display: inline-block; flex-shrink: 0; width: 104px; margin-left: 8px; color: #666; font-size: 11px; }
        .x6-edge:hover path:nth-child(2) { stroke: #1890ff; stroke-width: 1px; }
        .x6-edge-selected path:nth-child(2) { stroke: #1890ff; stroke-width: 1.5px !important; }
        .red { color: red; }
    </style>
</head>
<body style="width: 100%;margin:0 auto;overflow-x:hidden">
    <div id="container" style="height:450px;width:100%;">
        <div id="graph-container" style="width:100%;"></div>
    </div>
    <div class="layui-container" style="padding-top: 15px;background: #fff;">
        <div>
            <input type="text" id="search-input" class="layui-input layui-input-sm" style="width: 500px; clear: none; display: inline-block;" />
            <button type="button" class="layui-btn layui-btn-sm" onclick="search()">搜索</button> |
            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="showAll()" style="margin-left:0;">显示所有</button> |
            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="evelFormula()" style="margin-left:0;">计算公式</button>
        </div>
        <div id="formulasList">
            <table class="layui-table" lay-skin="line" lay-size="sm" id="list">
                <thead>
                    <tr> <th width="100">名称</th> <th>计算步骤</th> </tr>
                </thead>
            </table>
        </div>
    </div>
    <div style="height:50px;"></div>

    <script id="tpl" type="text/template">
        {{ for(var i=0;i < it.length; i++){ var item=it[i];}}
        {{ if(item.formulas && item.formulas.length==1 && item.formulas[0].text==item["value"] ) { }}
            <tr name="{{= item.name }}"><td><a href="javascript:;" onclick="showGraph('{{= item.nodeId }}','{{= item.name }}',)" style="font-weight:bold">{{= item.name }}</a></td><td>= {{= item["value"] }}</td></tr>
        {{ }else if(item.formulas) {  }}
        <tr name="{{= item.name }}">
            <td>
                <a href="javascript:;" onclick="showGraph('{{= item.nodeId }}','{{= item.name }}')" style="font-weight:bold">{{= item.name }}</a>
            </td>
            <td>= {{ for(var j=0;j< item.formulas.length;j++ ){ var f=item.formulas[j];
                if (f.text) {
            }}
            <span>{{= f.text }}</span>
            {{ }else{ }}
            <a href="javascript:;" onclick="showName('{{= f.name}}','{{= item.name }}','{{= item.nodeId }}')" style="color:blue" >{{= f.name}}</a>
            {{ } }}
            {{ } }}
            </td>
        </tr>
        <tr name="{{= item.name }}"><td></td>
            <td>=
                {{ for(var j=0;j< item.formulas.length;j++ ){ var f=item.formulas[j];
                if (f.text) {
            }}
            <span>{{= f.text }}</span>
            {{ }else{ }}
            <a href="javascript:;" onclick="showName('{{= f.name}}','{{= item.name }}','{{= item.nodeId }}')" >{{= f.value}}</a>
            {{ } }}
            {{ } }}
            </td>
        </tr>
        {{ if(item.formulas.length>1){ }}<tr name="{{= item.name }}"><td></td><td>= {{= item["value"] }}</td></tr>{{ } }}
        {{ }else{ }}
        <tr name="{{= item.name }}"><td><a href="javascript:;" onclick="showGraph('{{= item.nodeId }}','{{= item.name }}',)" style="font-weight:bold">{{= item.name }}</a></td><td>= {{= item["value"] }}</td></tr>
        {{ } }}
        <tr name="{{= item.name }}"><td style="padding:3px;"></td><td style="padding:3px;"></td></tr>
        {{ } }}
    </script>

    <script id="tpl2" type="text/template">
            <table class="layui-table" lay-skin="line" lay-size="sm" style="width:calc( 100% - 10px );margin: 5px;">
            <thead><tr><th width="100">名称</th><th>计算步骤</th></tr></thead>
            {{ if(it.conditions){ }}
            {{ for(var i=0;i < it.conditions.length;i++){ }}
            {{ var item=it.conditions[i];  }}
            <tr>
            {{ if(i==0){ }}<td rowspan="2">{{= it.name }}</td>{{ }else{ }}<td rowspan="2"></td>{{ } }}
            <td {{= item.check?'style="font-weight:bold"':'' }} >
                {{ for(var j=0;j< item.condition.length;j++){ }}
                {{ var citem=item.condition[j]; }}
                <span class="{{= citem.isError?'red':'' }}" >{{= citem.text }}</span>
                {{ } }}
            </td>
            </tr>
            <tr><td>{{= item.formula }}</td></tr>
            {{ } }}
            {{ if(it.defaultFormula){ }}<tr><td>默认公式</td><td>{{= it.defaultFormula }}</td></tr>{{ } }}
            {{ }else{ }}
            <tr><td>{{= it.name }}</td><td>{{= it.defaultFormula }}</td></tr>
            {{ } }}
            {{ if(it.comment && it.comment.length>0 ){ }}<tr><td>备注</td><td>{{= item.comment }}</td></tr>{{ } }}
        </table>
    </script>

    <script id="tpl3" type="text/template">
        <table class="layui-table" lay-skin="line" lay-size="sm" style="width:calc( 100% - 10px );margin: 5px;">
            {{ if(it.condition){ }}
            <tr>
                <td>条件</td>
                <td colspan="3">{{ for(var j=0;j< it.condition.length;j++){ var citem=it.condition[j]; }}<span class="{{= citem.isError?'red':'' }}" >{{= citem.text }}</span>{{ } }}</td>
            </tr>
            {{ } }}
            {{ if(it.procedure && it.procedure.name && it.procedure.code && it.procedure.categoryName && it.procedure.categoryCode   ){ }}
            <tr>
                <td>工艺</td>
                <td>{{= it.procedure.name||''  }} &nbsp; {{= it.procedure.code||''  }}  </td>
                <td>分类</td>
                <td>{{= it.procedure.categoryName ||'' }} &nbsp; {{= it.procedure.categoryCode ||'' }} </td>
            </tr>
            {{ } }}
            {{ if(it.procedure){ }}
            <tr>
                <td>条件</td>
                <td colspan="3">{{ for(var j=0;j< it.procedureCondition.length;j++){ var citem=it.procedureCondition[j]; }}<span class="{{= citem.isError?'red':'' }}" >{{= citem.text }}</span>{{ } }}</td>
            </tr>
            {{ } }}
            {{ if(it.machines){ }}
            {{ for(var i=0;i< it.machines.length;i++ ){ var machine=it.machines[i]; }}
            <tr><td colspan="4"></td></tr>
            <tr>
                <td>机械</td>
                <td>{{= machine.name||''  }} &nbsp; {{= machine.code||''  }} </td>
                <td>分类</td>
                <td>{{= machine.categoryName||''  }} &nbsp; {{= machine.categoryCode||''  }} </td>
            </tr>
            {{ if(machine.machineCondition && machine.machineCondition2){ }}
            <tr>
                <td rowspan="2">条件</td>
                <td colspan="3">{{ for(var j=0;j< machine.machineCondition.length;j++){ var citem=machine.machineCondition[j]; }}<span class="{{= citem.isError?'red':'' }}" >{{= citem.text }}</span>{{ } }}</td>
            </tr>
            <tr>
                <td colspan="3">{{ for(var j=0;j< machine.machineCondition2.length;j++){ var citem=machine.machineCondition2[j]; }}<span class="{{= citem.isError?'red':'' }}" >{{= citem.text }}</span>{{ } }}</td>
            </tr>
            {{ }else if(machine.machineCondition){ }}
            <tr>
                <td>条件</td>
                <td colspan="3">{{ for(var j=0;j< machine.machineCondition.length;j++){ var citem=machine.machineCondition[j]; }}<span class="{{= citem.isError?'red':'' }}" >{{= citem.text }}</span>{{ } }}</td>
            </tr>
            {{ }else if(machine.machineCondition2){ }}
            <tr>
                <td>条件</td>
                <td colspan="3"> {{ for(var j=0;j< machine.machineCondition2.length;j++){ var citem=machine.machineCondition2[j]; }} <span class="{{= citem.isError?'red':'' }}" >{{= citem.text }}</span> {{ } }}</td>
            </tr>
            {{ } }}
            {{ } }}
            {{ } }}
        </table>
    </script>

    <script src="/_/layui/layui.js"></script>
    <script src="/_/js/global.js"></script>
    <script src="/_/js/dot.min.js"></script>
    <script>
        function htmlDecode(str) { var temp = document.createElement("div"); temp.innerHTML = str; var output = temp.innerText || temp.textContent; temp = null; return output; }
        var appCode = '@(Model.AppCode)';
        var factoryCode = '@(Model.FactoryCode)';
        var json = htmlDecode('@(Model.Json)');
        var formulas = htmlDecode('@(Model.Formulas)');
        var machineInfos = htmlDecode('@(Model.MachineInfos)');
        var attachData = htmlDecode('@(Model.AttachData)');
        var previous = htmlDecode('@(Model.Previous)');
        var projectCode = htmlDecode('@(Model.ProjectCode)');

        Graph = X6.Graph;
        Shape = X6.Shape;
        Graph.registerEdge('dag-edge', { inherit: 'edge', attrs: { line: { stroke: '#C2C8D5', strokeWidth: 1, } } }, true)

        const graph = new Graph({
            grid: true,
            container: document.getElementById('graph-container'),
            panning: { enabled: true, eventTypes: ['leftMouseDown'] },
            mousewheel: { enabled: true, modifiers: ['ctrl', 'meta'], factor: 1.1, maxScale: 2, minScale: 0.5 },
            highlighting: { magnetAdsorbed: { name: 'stroke', args: { attrs: { fill: '#5F95FF', stroke: '#5F95FF', strokeWidth: 4 } } } },
            connecting: { snap: false, allowBlank: false, allowLoop: false, highlight: true, connectionPoint: 'anchor', anchor: 'center', validateMagnet({ magnet }) { return false; }, createEdge() { return graph.createEdge({ shape: 'dag-edge', zIndex: -1 }) }, },
            interacting: false,
            selecting: false,
            keyboard: false,
            clipboard: false,
            history: false,
            resizing: false,
            rotating: false,
            autoResize: true,
            snapline: false
        })
        const ports = { groups: { top: { position: 'left', attrs: { circle: { r: 4, magnet: true, stroke: '#5F95FF', } } }, bottom: { position: 'right', attrs: { circle: { r: 4, magnet: true, stroke: '#5F95FF' } } } }, items: [{ group: 'top' }, { group: 'bottom' }] }
        const endPorts = { groups: { top: { position: 'left', attrs: { circle: { r: 4, magnet: true, stroke: '#5F95FF', } } } }, items: [{ group: 'top' }] }
        const startPorts = { groups: { bottom: { position: 'right', attrs: { circle: { r: 4, magnet: true, stroke: '#5F95FF' } } } }, items: [{ group: 'bottom' }] }
        const noneports = { groups: {}, items: [] }
        Graph.registerNode('start', { inherit: 'rect', width: 66, height: 36, attrs: { body: { rx: 20, ry: 26, strokeWidth: 1, stroke: '#5F95FF', fill: '#EFF4FF' }, text: { fontSize: 12 } }, ports: { ...startPorts } }, true)
        Graph.registerNode('end', { inherit: 'circle', width: 36, height: 36, attrs: { body: { strokeWidth: 1, stroke: '#5F95FF', fill: '#EFF4FF' }, text: { fontSize: 12 } }, ports: { ...endPorts } }, true)
        Graph.registerNode('error', { inherit: 'circle', width: 36, height: 36, attrs: { body: { strokeWidth: 1, stroke: '#5F95FF', fill: '#FFDDFF' }, text: { fontSize: 12 } }, ports: { ...endPorts } }, true)
        Graph.registerNode('merge', { inherit: 'polygon', width: 66, height: 36, attrs: { body: { refPoints: '0,10 10,0 20,10 10,20', strokeWidth: 1, stroke: '#5F95FF', fill: '#EFF4FF' }, text: { fontSize: 12 } }, ports: { ...ports } }, true)
        Graph.registerNode('jump', { inherit: 'rect', width: 66, height: 36, attrs: { body: { strokeWidth: 1, stroke: '#5F95FF', fill: '#EFF4FF' }, text: { fontSize: 12 } }, ports: { ...ports } }, true)
        Graph.registerNode('procedure', { inherit: 'rect', width: 66, height: 36, attrs: { body: { strokeWidth: 1, stroke: '#5F95FF', fill: '#EFF4FF' }, text: { fontSize: 12 } }, ports: { ...ports } }, true)
        Graph.registerNode('procedureItem', { inherit: 'rect', width: 66, height: 36, attrs: { body: { strokeWidth: 1, stroke: '#5F95FF', fill: '#EFF4FF' }, text: { fontSize: 12 } }, ports: { ...ports } }, true)
        Graph.registerNode('custom', { inherit: 'rect', width: 66, height: 36, attrs: { body: { rx: 8, ry: 8, strokeWidth: 1, stroke: '#5F95FF', fill: '#EFF4FF' }, text: { fontSize: 12 } }, ports: { ...ports } }, true)
        Graph.registerNode('comment', { inherit: 'rect', width: 66, height: 36, attrs: { body: { strokeWidth: 1, stroke: '#fff', fill: '#fff' }, text: { fontSize: 14 }, label: { refX: 3, refY: 3, textAnchor: 'start', textVerticalAnchor: 'top', fill: '#ff4d4f' }, }, ports: { ...noneports } }, true)
        Graph.registerNode('status', { inherit: 'rect', width: 66, height: 36, attrs: { body: { strokeWidth: 1, stroke: '#5F95FF', fill: '#faffa4' }, text: { fontSize: 12 } }, ports: { ...ports } }, true)
        const showPorts = (ports, show) => { for (let i = 0, len = ports.length; i < len; i = i + 1) { ports[i].style.visibility = show ? 'visible' : 'hidden' } }
        
        function addNode(node) {
            var x = node.layer * 220 + 30;
            var y = node.order * 65 + 30;
            nd = graph.addNode(({ shape: node.nodeType, id: node.id, label: node.label, x: x, y: y }))
            if (node.nodeType == "procedure" || node.nodeType == "procedureItem" || node.nodeType == "jump" || node.nodeType == "custom" || node.nodeType == "status") { nd.resize(120, 36) }
        }
        function addError(node) {
            var x = node.layer * 220 + 30;
            var y = node.order * 65 + 30 + 40;
            graph.addNode(({ shape: 'comment', id: node.id + "_error", label: node.errorMessage, x: x, y: y }))
        }
        function addEdge(node) {
            if (node.nextNodeIds && graph.getCellById(node.id) && graph.getCellById(node.id).port && graph.getCellById(node.id).port.ports) {
                var ports = graph.getCellById(node.id).port.ports;
                var startPort = "";
                for (var i = 0; i < ports.length; i++) { if (ports[i].group == "bottom") { startPort = ports[i].id; break; } }
                for (var key in node.nextNodeIds) {
                    var ids = node.nextNodeIds[key];
                    for (var k = 0; k < ids.length; k++) {
                        var ports2 = graph.getCellById(ids[k]).port.ports;
                        for (var i = 0; i < ports2.length; i++) {
                            var post = ports2[i];
                            if (post.group == "top") {
                                graph.addEdge({ shape: 'dag-edge', source: { cell: node.id, port: startPort }, target: { cell: ids[k], port: post.id } })
                                break;
                            }
                        }
                    }
                }
            }
        }
        parameters = [];
        $(function () {
            var data = { appCode: appCode, factoryCode: factoryCode, json: json, previous: previous, attachData: attachData, machineInfos: machineInfos, projectCode: projectCode }
            var url = "/api/EvalWithMessages"
            $.post(url, data, function (r) {
                if (r.state == "SUCCESS") {
                    var nodes = r.data.nodes;
                    for (var i = 0; i < nodes.length; i++) {
                        addNode(nodes[i]);
                        if (nodes[i].isError && nodes[i].errorMessage) { addError(nodes[i]); }
                    }
                    for (var i = 0; i < nodes.length; i++) { addEdge(nodes[i]); }
                    showPorts(document.getElementById('graph-container').querySelectorAll('.x6-port-body'), false)
                    parameters = r.data.parameters;
                    var addFormulas = r.data.parameters.join(",")
                    var data = { appCode: appCode, factoryCode: factoryCode, json: json, attachData: attachData, machineInfos: machineInfos, formulas: formulas, addFormulas: addFormulas, projectCode: projectCode }
                    var url = "/api/EvalFormulaDetails"
                    if (r.data.isError) { var url = "/api/EvalFormulaDetailsWithError" }
                    $.post(url, data, function (r) {
                        if (r.code == 1) {
                            var tpl = doT.template($("#tpl").text());
                            $("#list").append(tpl(r.data.exps))
                            $("#list").append(tpl(r.data.items));
                        } else {
                            $("#list").append('<tr><td colspan="2" style="color:red">' + r.message + '</td></tr>')
                        }
                    })
                } else {
                    layui.layer.alert(r.message);
                }
            })
        })
        graph.on('node:click', ({ e, x, y, node, view }) => {
            if (node.shape == "comment") {
                var text = node.attr().text.text;
                var list = [];
                for (var i = 0; i < parameters.length; i++) {
                    if (text.indexOf(parameters[i]) > -1) {
                        list.push(parameters[i]);
                    }
                }
                $("#search-input").val(list.join(' '));
                search();
            }else if (node.shape == "error" || node.shape == "jump" || node.shape == "custom" || node.shape == "procedure") {
                var url = "/api/EvalNodeConditionDetails";
                var data = { appCode: appCode, factoryCode: factoryCode, json: json, previous: previous, nodeid: node.id, projectCode:projectCode }
                $.post(url, data, function (r) {
                    if (r.data) {
                        if (!r.data.condition && !r.data.procedureCondition && !r.data.machines) {
                            layui.layer.msg("此节点无条件控制！");
                            return
                        }
                        var tpl = doT.template($("#tpl3").text());
                        var html = tpl(r.data)
                        layui.layer.open({
                            type: 1,
                            area: ['900px', '700px'],
                            title: r.data.label,
                            shadeClose: true,
                            content: html
                        });
                    } else {
                        layui.layer.msg(r.message);
                    }
                })
            } else {
                layui.layer.msg("节点不支持！");
            }
        });
    </script>

    <script>
        $(function () { $("#search-input").keyup(function (key) { if (key.keyCode == 13) { search(); return false; } }); })
        function search() { var val = $("#search-input").val().toUpperCase(); $("tr[name]").each(function () { if ($(this).attr("name").toUpperCase().indexOf(val) > -1) { $(this).show(); } else { $(this).hide(); } }) }
        var _name = false;
        var _startName = false;
        var _show = true;
        function showAll() {
            $("tr[name]").show(); _show = true;
            var nodes = graph.getNodes();
            for (var i = 0; i < nodes.length; i++) {
                var node = nodes[i];
                if (node.shape != "comment") { node.setAttrs({ body: { fill: '#EFF4FF' } }) }
            }
        }

        function showName(name, startName, id) {
            var b = false;
            if (_name != name || _startName != startName) { _show = true; }

            $("tr[name]").each(function () {
                var name2 = $(this).attr("name");
                if (name2 == name) {
                    $(this).show();
                } else if (b && name2 != startName) {
                    if (_show) { $(this).hide(); } else { $(this).show(); }
                }
                if (name2 == startName) { b = true; }
            }).end()

            if (_name == name && _startName == startName) { _show = !_show; } else { _show = false; _name = name; _startName = startName; }
            var nodes = graph.getNodes();
            for (var i = 0; i < nodes.length; i++) {
                var node = nodes[i];
                if (node.id == id) {
                    node.setAttrs({ body: { fill: 'rgb(95, 149, 255)' } })
                } else if (node.shape != "comment") {
                    node.setAttrs({ body: { fill: '#EFF4FF' } })
                }
            }
        }
    </script>

    <script>
        layui.use(["layer", 'element', "form", "table"], function () { });
        function showGraph(id, name) {
            var nodes = graph.getNodes();
            for (var i = 0; i < nodes.length; i++) {
                var node = nodes[i];
                if (node.id == id) {
                    node.setAttrs({ body: { fill: 'rgb(95, 149, 255)' } })
                } else if (node.shape != "comment") {
                    node.setAttrs({ body: { fill: '#EFF4FF' } })
                }
            }
            if (id == "undefined") return;
            var url = "/api/EvalConditionDetails";
            var data = { appCode: appCode, factoryCode: factoryCode, json: json, previous: previous, nodeid: id, name: name, projectCode: projectCode }
            $.post(url, data, function (r) {
                if (r.data) {
                    var tpl = doT.template($("#tpl2").text());
                    var html = tpl(r.data)
                    layui.layer.open({
                        type: 1,
                        area: ['900px', '700px'],
                        title: '公式详情',
                        shadeClose: true,
                        content: html
                    });
                }
            })
        }
    </script>
    <script>
        function evelFormula() {
            formulas = $("#search-input").val();
            if (formulas == "") {
                layui.layer.msg("请输入公式");
                return
            }
            var url = "./FlowJumpUrl?url=./FlowFormulaDetailsResult";
            layui.layer.open({
                type: 2,
                area: ['900px', '700px'],
                title: '公式详情',
                shadeClose: true,
                content: url
            });
        }
    </script>
    @if (MyHostingEnvironment.IsDevelopment == false) {
        <script>setInterval(function () { check() }, 1000); var check = function () { function doCheck(a) { if (("" + a / a)["length"] !== 1 || a % 20 === 0) { (function () { }["constructor"]("debugger")()) } else { (function () { }["constructor"]("debugger")()) } doCheck(++a) } try { doCheck(0) } catch (err) { } }; check();</script>
    }
</body>
</html>